package com.wanke.dao.impl;
// Generated 2016-12-1 14:23:46 by Hibernate Tools 4.0.1.Final

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Example;
import org.springframework.stereotype.Repository;

import com.wanke.bean.QueryCheckDormitoryCondition;
import com.wanke.dao.CheckinDormitoryDao;
import com.wanke.entity.CheckinDormitory;
import com.wanke.entity.Student;

/**
 * Home object for domain model class CheckinDormitory.
 * 
 * @see com.wanke.entity.CheckinDormitory
 * @author Hibernate Tools
 */
@Repository("checkinDormitoryDao")
public class CheckinDormitoryDaoImpl implements CheckinDormitoryDao {

	private static final Log log = LogFactory.getLog(CheckinDormitoryDaoImpl.class);
	DateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	Date time=new Date();

	@Resource(name = "sessionFactory")
	SessionFactory sessionFactory;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wanke.dao.CheckinDormitoryDao#persist(com.wanke.entity.
	 * CheckinDormitory)
	 */
	@Override
	public void persist(CheckinDormitory transientInstance) {
		log.debug("persisting CheckinDormitory instance");
		try {
			sessionFactory.getCurrentSession().persist(transientInstance);
			log.debug("persist successful");
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wanke.dao.CheckinDormitoryDao#attachDirty(com.wanke.entity.
	 * CheckinDormitory)
	 */
	@Override
	public void attachDirty(CheckinDormitory instance) {
		log.debug("attaching dirty CheckinDormitory instance");
		try {
			sessionFactory.getCurrentSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wanke.dao.CheckinDormitoryDao#attachClean(com.wanke.entity.
	 * CheckinDormitory)
	 */
	@Override
	public void attachClean(CheckinDormitory instance) {
		log.debug("attaching clean CheckinDormitory instance");
		try {
			sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wanke.dao.CheckinDormitoryDao#delete(com.wanke.entity.
	 * CheckinDormitory)
	 */
	@Override
	public void delete(CheckinDormitory persistentInstance) {
		log.debug("deleting CheckinDormitory instance");
		try {
			sessionFactory.getCurrentSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wanke.dao.CheckinDormitoryDao#merge(com.wanke.entity.
	 * CheckinDormitory)
	 */
	@Override
	public CheckinDormitory merge(CheckinDormitory detachedInstance) {
		log.debug("merging CheckinDormitory instance");
		try {
			CheckinDormitory result = (CheckinDormitory) sessionFactory.getCurrentSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wanke.dao.CheckinDormitoryDao#findById(int)
	 */
	@Override
	public CheckinDormitory findById(int id) {
		log.debug("getting CheckinDormitory instance with id: " + id);
		try {
			CheckinDormitory instance = (CheckinDormitory) sessionFactory.getCurrentSession()
					.get("com.wanke.entity.CheckinDormitory", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wanke.dao.CheckinDormitoryDao#findByExample(com.wanke.entity.
	 * CheckinDormitory)
	 */
	@Override
	public List findByExample(CheckinDormitory instance) {
		log.debug("finding CheckinDormitory instance by example");
		try {
			List results = sessionFactory.getCurrentSession().createCriteria("com.wanke.entity.CheckinDormitory")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	@Override
	public boolean saveCheckinDormitory(CheckinDormitory checkinDormitory) {
		boolean result = false;
		try {
			sessionFactory.getCurrentSession().save(checkinDormitory);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public boolean updateCheckinDormitory(CheckinDormitory checkinDormitory) {
		boolean result = false;
		try {
			sessionFactory.getCurrentSession().update(checkinDormitory);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@Override
	public List<CheckinDormitory> findAllCheckinDormitory() {
		String hql = "from CheckinDormitory as ck";
		List<CheckinDormitory> checkinDormitories = sessionFactory.getCurrentSession().createQuery(hql).list();
		return checkinDormitories;
	}

	@Override
	public List<CheckinDormitory> findByTime(String time) {
		String hql = "from CheckinDormitory as ck where Date(ck.time) =:time";
		List<CheckinDormitory> cds = sessionFactory.getCurrentSession().createQuery(hql).setParameter("time", time)
				.list();
		return cds;
	}

	@Override
	public List<CheckinDormitory> findByTimeAndStudent(String time, Set<Student> students) {
		String hql = "from CheckinDormitory as cd where Date(cd.time) =:time and cd.students=:students";
		List<CheckinDormitory> cds = sessionFactory.getCurrentSession().createQuery(hql).setParameter("time", time)
				.setParameter("students", students).list();
		return cds;
	}

	
	
	
	
	
	
	
	
	
	
	
	@Override
	public List<CheckinDormitory> findByTimeBanjiNameAsType1(QueryCheckDormitoryCondition condition) {
		
		String hql = "from CheckinDormitory as cd where cd.headTeacherId=:headTeacherId and cd.studentName=:studentName and cd.banjiName=:banjiName and Date(cd.time)=:time";
		try {
			 time = sdf.parse(condition.getTime().toString()); 
		} catch (ParseException e) {
			e.printStackTrace();
		}
		List<CheckinDormitory> cds = sessionFactory.getCurrentSession().createQuery(hql)
				.setParameter("headTeacherId", condition.getTeacher_id())
				.setParameter("studentName", condition.getStudent_name())
				.setParameter("banjiName", condition.getBanji_name()).setParameter("time", time).list();
		return cds;
	}

	@Override
	public List<CheckinDormitory> findByTimeBanjiAsType1(QueryCheckDormitoryCondition condition) {
		System.out.println(getClass().toString()+"-->"+condition.toString());
		String hql = "from CheckinDormitory as cd where cd.headTeacherId=:headTeacherId and cd.banjiName=:banjiName and Date(cd.time)=:time";
		 try {
			 time = sdf.parse(condition.getTime().toString()); 
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		List<CheckinDormitory> cds = sessionFactory.getCurrentSession().createQuery(hql)
				.setParameter("headTeacherId", condition.getTeacher_id())
				.setParameter("banjiName", condition.getBanji_name())
				.setParameter("time",time)
				.list();
		return cds;
	}

	@Override
	public List<CheckinDormitory> findByTimeBanjiNameAsType2(QueryCheckDormitoryCondition condition) {
		String hql = "from CheckinDormitory as cd where cd.studentName=:studentName and cd.zoom1=:zoom1 and cd.zoom2=:zoom2 and Date(cd.time)=:time";
		try {
			 time = sdf.parse(condition.getTime().toString()); 
		} catch (ParseException e) {
			e.printStackTrace();
		}
		List<CheckinDormitory> cds = sessionFactory.getCurrentSession().createQuery(hql)
				
				.setParameter("studentName", condition.getStudent_name()).setParameter("zoom1", condition.getZoom_1())
				.setParameter("zoom2", condition.getZoom_2()).setParameter("time", time).list();
		return cds;
	}

	@Override
	public List<CheckinDormitory> findByTimeBanjiAsType2(QueryCheckDormitoryCondition condition) {
		String hql = "from CheckinDormitory as cd where cd.zoom1=:zoom1 and cd.zoom2=:zoom2 and Date(cd.time)=:time";
		try {
			 time = sdf.parse(condition.getTime().toString()); 
		} catch (ParseException e) {
			e.printStackTrace();
		}
		List<CheckinDormitory> cds = sessionFactory.getCurrentSession().createQuery(hql)
				.setParameter("zoom1", condition.getZoom_1())
				.setParameter("zoom2", condition.getZoom_2()).setParameter("time", time).list();
		return cds;
	}

	@Override
	public List<CheckinDormitory> findByTimeBanjiNameAsType3(QueryCheckDormitoryCondition condition) {
		String hql = "from CheckinDormitory as cd where cd.studentName=:studentName and cd.banjiName=:banjiName and Date(cd.time)=:time";
		try {
			 time = sdf.parse(condition.getTime().toString()); 
		} catch (ParseException e) {
			e.printStackTrace();
		}
		List<CheckinDormitory> cds = sessionFactory.getCurrentSession().createQuery(hql)
				.setParameter("studentName", condition.getStudent_name())
				.setParameter("banjiName", condition.getBanji_name()).setParameter("time", time).list();
		return cds;
	}

	@Override
	public List<CheckinDormitory> findByTimeBanjiAsType3(QueryCheckDormitoryCondition condition) {
		String hql = "from CheckinDormitory as cd where cd.banjiName=:banjiName and Date(cd.time)=:time";
		try {
			 time = sdf.parse(condition.getTime().toString()); 
		} catch (ParseException e) {
			e.printStackTrace();
		}
		List<CheckinDormitory> cds = sessionFactory.getCurrentSession().createQuery(hql)
				.setParameter("banjiName", condition.getBanji_name()).setParameter("time", time).list();
		return cds;
	}

	
	
	
	
	
	
	
	
	
	
	
	
	@Override
	public List<CheckinDormitory> findBySemesterNameAsType1(QueryCheckDormitoryCondition condition) {
		String hql = "from CheckinDormitory as cd where cd.headTeacherId=:headTeacherId and cd.studentName=:studentName and cd.banjiName=:banjiName and cd.semester=:semester";
		List<CheckinDormitory> cds = sessionFactory.getCurrentSession().createQuery(hql)
				.setParameter("headTeacherId", condition.getTeacher_id())
				.setParameter("studentName", condition.getStudent_name())
				.setParameter("banjiName", condition.getBanji_name()).setParameter("semester", condition.getSemester())
				.list();
		return cds;
	}

	@Override
	public List<CheckinDormitory> findBySemesterAsType1(QueryCheckDormitoryCondition condition){
		System.out.println(getClass().toString()+"-->"+"right dao");
		String hql = "from CheckinDormitory as cd where cd.headTeacherId=:headTeacherId  and cd.banjiName=:banjiName and cd.semester=:semester";
		List<CheckinDormitory> cds =new ArrayList<>();
		try {
			cds = sessionFactory.getCurrentSession().createQuery(hql)
					.setParameter("headTeacherId", condition.getTeacher_id())
					.setParameter("banjiName", condition.getBanji_name()).setParameter("semester", condition.getSemester())
					.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cds;
	}

	@Override
	public List<CheckinDormitory> findBySemesterNameAsType2(QueryCheckDormitoryCondition condition) {
		String hql = "from CheckinDormitory as cd where  cd.studentName=:studentName and cd.zoom1=:zoom1 and cd.zoom2=:zoom2 and cd.semester=:semester";
		List<CheckinDormitory> cds = sessionFactory.getCurrentSession().createQuery(hql)
				
				.setParameter("studentName", condition.getStudent_name()).setParameter("zoom1", condition.getZoom_1())
				.setParameter("zoom2", condition.getZoom_2()).setParameter("semester", condition.getSemester()).list();
		return cds;
	}

	@Override
	public List<CheckinDormitory> findBySemesterAsType2(QueryCheckDormitoryCondition condition) {
		String hql = "from CheckinDormitory as cd where cd.zoom1=:zoom1 and cd.zoom2=:zoom2 and cd.semester=:semester";
		List<CheckinDormitory> cds = sessionFactory.getCurrentSession().createQuery(hql)
				.setParameter("zoom1", condition.getZoom_1())
				.setParameter("zoom2", condition.getZoom_2()).setParameter("semester", condition.getSemester()).list();
		return cds;
	}

	@Override
	public List<CheckinDormitory> findBySemesterNameAsType3(QueryCheckDormitoryCondition condition) {
		String hql = "from CheckinDormitory as cd where cd.studentName=:studentName and cd.banjiName=:banjiName and cd.semester=:semester";
		List<CheckinDormitory> cds = sessionFactory.getCurrentSession().createQuery(hql)
				.setParameter("studentName", condition.getStudent_name())
				.setParameter("banjiName", condition.getBanji_name()).setParameter("semester", condition.getSemester()).list();
		return cds;
	}

	@Override
	public List<CheckinDormitory> findBySemesterAsType3(QueryCheckDormitoryCondition condition) {
		String hql = "from CheckinDormitory as cd where cd.banjiName=:banjiName and cd.semester=:semester";
		List<CheckinDormitory> cds = sessionFactory.getCurrentSession().createQuery(hql)
				.setParameter("banjiName", condition.getBanji_name()).setParameter("semester", condition.getSemester()).list();
		return cds;
	}

}
